#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   App_TesterPresent_0x3E_test.py
@Time    :   2022/09/06 13:31:28
@Author  :   YangLiang 
@Version :   1.0
@Contact :   yangliang@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
from pickle import TRUE
from pytest_assume.plugin import assume

import pytest
import time
import requests
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
from collections.abc import Iterable

from sqlalchemy import false
sys.dont_write_bytecode = True

TesterPresent_NRC13 = [('3E','7F 3E 13'),
                       ('3E 00 01','7F 3E 13'),
                       ('3E 05 01','7F 3E 13'),
                       ('3E 80 01','7F 3E 13'),
                       ('3E 81 01','7F 3E 13'),
                       ('3E 85 01','7F 3E 13')
                      ]

TesterPresent_NRC12 = [('3E 01','7F 3E 12'),
                       ('3E 02','7F 3E 12'),
                       ('3E 20','7F 3E 12'),
                       ('3E 7F','7F 3E 12'),
                       ('3E 81','7F 3E 12'),
                       ('3E 8A','7F 3E 12')
                      ]
# 20220913 Revise:BYD 企标规定 功能寻址 NRC=11 12 13 7E 7F 31都不响应
TesterPresent_NRC13_Fun = [([0x3E], []),               #[0x7F, 0x3E, 0x13]),
                           ([0x3E, 0x00, 0x01], []),   #[0x7F, 0x3E, 0x13]),
                           ([0x3E, 0x05, 0x01], [])]   #[0x7F, 0x3E, 0x13])]

TesterPresent_No_Resp_Fun = [([0x3E, 0x01], []),
                             ([0x3E, 0x20], []),
                             ([0x3E, 0x7F], [])
                             ]
# 20220913 Revise:BYD 企标规定 功能寻址 NRC=11 12 13 7E 7F 31都不响应
TesterPresent_SuppressPosMsg_NRC13_Fun = [([0x3E, 0x80, 0x01], []),  #[0x7F, 0x3E, 0x13]),
                                          ([0x3E, 0x85, 0x01], [])]  #[0x7F, 0x3E, 0x13])]

TesterPresent_SuppressPosMsg_No_Resp_Fun = [([0x3E, 0x81], []),
                                            ([0x3E, 0xA0], []),
                                            ([0x3E, 0xFF], [])
                                            ]


class Test_TesterPresent_Phy(object):

    @allure.severity('normal')
    @allure.story('Tester Present Test')
    @allure.description('Test positive response of 0x3E with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    def test_TesterPresent_default_Phy(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "3E00", "7E00", 1, iStep)

    @allure.severity('normal')
    @allure.story('Tester Present Test')
    @allure.description('Test positive response of 0x3E with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    def test_TesterPresent_extended_Phy(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "3E00", "7E00", 3, iStep)

    @allure.severity('normal')
    @allure.story('Tester Present Test')
    @allure.description('Test positive response of 0x3E with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    def test_TesterPresent_programming_Phy(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "3E00", "7E00", 2, iStep)





    ids = (f"{req}" for req, resp in TesterPresent_NRC13)
    @allure.severity('minor')
    @allure.description('Test NRC13 of 0x3E with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp', TesterPresent_NRC13, ids=ids)
    def test_TesterPresent_NRC13_default_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1,iStep)


    @allure.severity('minor')
    @allure.description('Test NRC13 of 0x3E with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp', TesterPresent_NRC13, ids=ids)
    def test_TesterPresent_NRC13_extended_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3,iStep)


    @allure.severity('minor')
    @allure.description('Test NRC13 of 0x3E with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp', TesterPresent_NRC13, ids=ids)
    def test_TesterPresent_NRC13_programming_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2,iStep)



    ids = (f"{req}" for req, resp in TesterPresent_NRC12)
    @allure.severity('minor')
    @allure.description('Test NRC12 of 0x3E with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp', TesterPresent_NRC12, ids=ids)
    def test_TesterPresent_NRC12_default_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1,iStep)


    @allure.severity('minor')
    @allure.description('Test NRC12 of 0x3E with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp', TesterPresent_NRC12, ids=ids)
    def test_TesterPresent_NRC12_extended_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3,iStep)
    
    @allure.severity('minor')
    @allure.description('Test NRC12 of 0x3E with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp', TesterPresent_NRC12, ids=ids)
    def test_TesterPresent_NRC12_programming_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2,iStep, True)




    @allure.severity('normal')
    @allure.description('Test suppressPosMsgBit is enabled 0x3E in default session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    def test_TesterPresent_SuppressPosMsgBit_default_Phy(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "3E80", "", 1, iStep)


    @allure.severity('normal')
    @allure.description('Test suppressPosMsgBit is enabled 0x3E in default session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    def test_TesterPresent_SuppressPosMsgBit_extended_Phy(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "3E80", "", 3, iStep)

    @allure.severity('normal')
    @allure.description('Test suppressPosMsgBit is enabled 0x3E in default session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    def test_TesterPresent_SuppressPosMsgBit_programming_Phy(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "3E80", "", 2, iStep, True)



class Test_TesterPresent_Fun(object):

    @allure.severity('normal')
    @allure.story('Tester Present Test')
    @allure.description('Test positive response of 0x3E in default session with function addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    def test_TesterPresent_Default_Fun(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x3E, 0x00], [0x7E, 0x00], 1, iStep, False, 'UDSFun', 'noClearFault')


    @allure.severity('normal')
    @allure.story('Tester Present Test')
    @allure.description('Test positive response of 0x3E in programming session with function addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    def test_TesterPresent_Programming_Fun(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "3E00", "7E00", 2, iStep, False, 'UDSFun')
        time.sleep(1.5)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "1001", "5001", 0, iStep, False, 'UDSFun')

    @allure.severity('normal')
    @allure.description('Test positive response of 0x3E in extended session with function addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
#    Test postive response of 0x3E in default session with function addressing
    def test_TesterPresent_Extended_Fun(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x3E, 0x00], [0x7E, 0x00], 3, iStep, False, 'UDSFun', 'noClearFault')


    ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in TesterPresent_NRC13_Fun]
    @allure.severity('minor')
    @allure.description('Test NRC13 of 0x3E in default session with function addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp', TesterPresent_NRC13_Fun, ids=ids)
    def test_TesterPresent_Default_NRC13_Fun(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x01], [0x50, 0x01], 0, iStep, False, 'UDSFun', 'noClearFault')
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, [], 0, iStep, False, 'UDSFun', 'noClearFault')

        

    ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in TesterPresent_No_Resp_Fun]
    @allure.description('Test NRC13 of 0x3E in default session with function addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp', TesterPresent_No_Resp_Fun, ids=ids)
    def test_TesterPresent_Default_No_Resp_Fun(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x01], [0x50, 0x01], 0, iStep, False, 'UDSFun', 'noClearFault')
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, [], 0, iStep, False, 'UDSFun', 'noClearFault')

                

    ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in TesterPresent_NRC13_Fun]
    @allure.severity('minor')
    @allure.description('Test NRC13 of 0x3E in extended session with function addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp', TesterPresent_NRC13_Fun, ids=ids)
    def test_TesterPresent_Extended_NRC13_Fun(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x03], [0x50, 0x03], 0, iStep, False, 'UDSFun', 'noClearFault')
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, [], 0, iStep, False, 'UDSFun', 'noClearFault')

       


    ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in TesterPresent_NRC13_Fun]
    @allure.severity('minor')
    @allure.description('Test NRC13 of 0x3E in Programming session with function addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp', TesterPresent_NRC13_Fun, ids=ids)
    def test_TesterPresent_Programming_NRC13_Fun(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, [], 2, iStep, False, 'UDSFun', 'noClearFault')

    
    
    ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in TesterPresent_No_Resp_Fun]
    @allure.severity('minor')
    @allure.description('Test no response of 0x3E in extended session with function addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp', TesterPresent_No_Resp_Fun, ids=ids)
    def test_TesterPresent_Extended_No_Resp_Fun(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x03], [0x50, 0x03], 0, iStep, False, 'UDSFun', 'noClearFault')
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, [], 0, iStep, False, 'UDSFun', 'noClearFault')


    @allure.severity('normal')
    @allure.description('Test suppressPosMsgBit is enabled 0x3E in default session with function addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    def test_TesterPresent_SuppressPosMsgBit_Default_Fun(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x01], [0x50, 0x01], 0, iStep, False, 'UDSFun', 'noClearFault')
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x3E, 0x80], [], 0, iStep, False, 'UDSFun', 'noClearFault')


    @allure.severity('normal')
    @allure.description('Test suppressPosMsgBit is enabled of 0x3E in extended session with function addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    def test_TesterPresent_SuppressPosMsgBit_Extended_Fun(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x03], [0x50, 0x03], 0, iStep, False, 'UDSFun', 'noClearFault')
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x3E, 0x80], [], 0, iStep, False, 'UDSFun', 'noClearFault')

    @allure.severity('normal')
    @allure.description('Test suppressPosMsgBit is enabled of 0x3E in programming session with function addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    def test_TesterPresent_SuppressPosMsgBit_Programming_Fun(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "3E80", "", 2, iStep, True, 'UDSFun')

        

    ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in TesterPresent_SuppressPosMsg_NRC13_Fun]
    @allure.severity('minor')
    @allure.description('Test negative response code of 0x3E when suppressPosMsgBit is enabled in default session with function addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp', TesterPresent_SuppressPosMsg_NRC13_Fun, ids=ids)
    def test_TesterPresent_SuppressPosMsg_Default_NRC13_Fun(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x01], [0x50, 0x01], 0, iStep, False, 'UDSFun', 'noClearFault')
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, [], 0, iStep, False, 'UDSFun', 'noClearFault')



    ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in TesterPresent_SuppressPosMsg_No_Resp_Fun]
    @allure.severity('minor')
    @allure.description('Test no response of 0x3E when suppressPosMsgBit is enabled in default session with function addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp', TesterPresent_SuppressPosMsg_No_Resp_Fun, ids=ids)
    def test_TesterPresent_SuppressPosMsg_Default_No_Resp_Fun(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x01], [0x50, 0x01], 0, iStep, False, 'UDSFun', 'noClearFault')
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, [], 0, iStep, False, 'UDSFun', 'noClearFault')



    ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in TesterPresent_SuppressPosMsg_NRC13_Fun]
    @allure.severity('minor')
    @allure.description('Test negative response code of 0x3E when suppressPosMsgBit is enabled in extended session with function addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp', TesterPresent_SuppressPosMsg_NRC13_Fun, ids=ids)
    def test_TesterPresent_SuppressPosMsg_Extended_NRC13_Fun(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x03], [0x50, 0x03], 0, iStep, False, 'UDSFun', 'noClearFault')
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, [], 0, iStep, False, 'UDSFun', 'noClearFault')

    @allure.severity('minor')
    @allure.description('Test negative response code of 0x3E when suppressPosMsgBit is enabled in programming session with function addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp', TesterPresent_SuppressPosMsg_NRC13_Fun, ids=ids)
    def test_TesterPresent_SuppressPosMsg_Programming_NRC13_Fun(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, [], 2, iStep, False, 'UDSFun', 'noClearFault')
        
        

    ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in TesterPresent_SuppressPosMsg_No_Resp_Fun]
    @allure.severity('minor')
    @allure.description('Test no response of 0x3E when suppressPosMsgBit is enabled in extended session with function addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp', TesterPresent_SuppressPosMsg_No_Resp_Fun, ids=ids)
    def test_TesterPresent_SuppressPosMsg_Extended_No_Resp_Fun(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x03], [0x50, 0x03], 0, iStep, False, 'UDSFun', 'noClearFault')
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, [], 0, iStep, False, 'UDSFun', 'noClearFault')



    @allure.severity('minor')
    @allure.description('Test no response of 0x3E when suppressPosMsgBit is enabled in programming session with function addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp', TesterPresent_SuppressPosMsg_No_Resp_Fun, ids=ids)
    def test_TesterPresent_SuppressPosMsg_Programming_No_Resp_Fun(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, [], 2, iStep, False, 'UDSFun', 'noClearFault')
